//package com.will.txj.aj.security.config.tmp;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.jcraft.jsch.UserInfo;
//import com.will.txj.common.result.R;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.ArrayList;
//
///**
// * @author: wen-yi;
// * @date: 2021/12/6 17:19;
// * @Description:
// */
//@Component
//public class TokenLoginFilter extends UsernamePasswordAuthenticationFilter {
//    @Autowired
//    JwtTokenUtils jwtTokenUtils;
//
//    public TokenLoginFilter() {
//        this.setPostOnly(false);
//        this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login","POST"));
//    }
//
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//        //获取表单提交数据
//        try {
//            UserInfo user = new ObjectMapper().readValue(request.getInputStream(), UserInfo.class);
//            return super.getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(user.getLoginName(),user.getPassword(),
//                    new ArrayList<>()));
//        } catch (IOException e) {
//            e.printStackTrace();
//            throw new RuntimeException();
//        }
//    }
//
//    @Override
//    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
//        UserSecurity userSecurity = (UserSecurity) authResult.getPrincipal();
//        String token = jwtTokenUtils.createToken(userSecurity.getUsername());
//        ResponseUtils.out(response, R.ok(token));
//    }
//
//    @Override
//    protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) throws IOException, ServletException {
//        ResponseUtils.out(response, R.fail(ServiceError.LOGIN_FAIL));
//    }
//
//    @Autowired
//    @Override
//    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
//        super.setAuthenticationManager(authenticationManager);
//    }
//}
